import $http from "@/services";
export default {
    namesapce: "common",
    state: {
        CollArrow: false,
        isShowDetail: false,
        isShowAdd: false,
        ids: [],
        isClearForm: false
    },
    reducers: {
        changeColl(state, { payload }) {
            return { ...state, CollArrow: payload }
        },
        setIsShowDetail(state, { payload }) {
            return { ...state, isShowDetail: payload }
        },
        setIsShowAdd(state, { payload }) {
            return { ...state, isShowAdd: payload }
        },
        setdeleteIds(state, { payload }) {
            return { ...state, ids: payload }
        },
        setIsClearForm(state, { payload }) {
            return { ...state, isClearForm: payload }
        },
    },
    effects: {
        *queryLogin({ payload }, { call }) {
            const { history, history: { location: { pathname } } } = payload;
            // 判断是否有其user如果有直接跳转
            if (!pathname.includes("/user")) {
                // 验证未通过的情况
                if (!localStorage.getItem("token") ||
                    !localStorage.getItem("userProfile") ||
                    !localStorage.getItem("routeList")) {
                    history.push("/users/login")
                } else {
                    const result = yield call($http.queryLoginStatus);
                    if (result.code !== 0) {
                        return;
                    }
                }
            } else {
                localStorage.clear();
            }
        }
    },
    subscriptions: {
        setUp({ dispatch, history }) {
            dispatch({
                type: "queryLogin",
                payload: { history }
            })
        }
    }
}